import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';

Future showUpBottomSheetView(
  BuildContext context, {
  bool enableDrag = true,
  bool expand = false,
  bool isDismissible = true,
  bool isShowIndicator = false,
  double spacingSpacing = 200,
  double? borderRadius,
  Color contentColor = Colors.white,
  required Widget child,
  Widget? header, //如果设置了header 下面关于header的参数就失效了
}) {
  return showMaterialModalBottomSheet(
    context: context,
    clipBehavior: Clip.antiAlias,
    backgroundColor: Colors.transparent,
    expand: expand,
    enableDrag: enableDrag,
    isDismissible: isDismissible,
    builder: (context) => UpBottomSheet(
      spacingSpacing: spacingSpacing,
      borderRadius: borderRadius,
      contentColor: contentColor,
      isShowIndicator: isShowIndicator,
      header: header,
      child: child,
    ),
  );
}

class UpBottomSheet extends StatelessWidget {
  const UpBottomSheet({
    Key? key,
    this.spacingSpacing,
    required this.child,
    this.header,
    this.borderRadius,
    this.isShowIndicator = false,
    required this.contentColor,
  }) : super(key: key);

  final Widget? header;
  final Widget child;
  final double? borderRadius;
  final Color contentColor;
  final bool isShowIndicator;

  /// 预留空间
  final double? spacingSpacing;

  @override
  Widget build(BuildContext context) {
    return Material(
      color: contentColor,
      borderRadius: BorderRadius.only(
        topLeft: Radius.circular(borderRadius ?? 10),
        topRight: Radius.circular(borderRadius ?? 10),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (isShowIndicator)
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                height: 4,
                width: 32,
                margin: const EdgeInsets.only(bottom: 8, top: 10),
                decoration: BoxDecoration(
                  color: const Color(0xffD8D8D8),
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
            ),
          if (header != null) header!,
          ConstrainedBox(
            constraints: BoxConstraints(
              maxHeight:
                  MediaQuery.of(context).size.height - (spacingSpacing ?? 200),
            ),
            child: SingleChildScrollView(
              child: child,
            ),
          ),
        ],
      ),
    );
  }
}
